Ext.onReady(function(){
	//Ext.Ajax.defaultHeaders +=',charset=UTF-8';
	
	//自定义
	var data=[
		{id:1,name:'小王',email:'xiaowang@easyjf.com',sex:'男',bornDate:'1991-4-4'},
		{id:2,name:'小李',email:'xiaoli@easyjf.com',sex:'男',bornDate:'1992-5-6'},
		{id:3,name:'小兰',email:'xiaoxiao@easyjf.com',sex:'女',bornDate:'1993-3-7'} 
	];
	//自定义 
	var mystore=new Ext.data.JsonStore({
		data:data,
		fields:["id","name","sex","email",{name:"bornDate",type:"date",dateFormat:"Y-n-j"}]
	});
	
	var store=new Ext.data.JsonStore({
		url: 'music/loadMusic.wkm',
    	root: 'roots',
    	fields: [
    		{name:'musicId',type:'string'},
    		{name:'title',type:'string'},
    		{name:'singer',type:'string'},
    		{name:'address',type:'string'},
    		{name:'lyrics',type:'string'}
    	]
	});
	
	store.load();
	//setTimeout(function(){},3000);
	
	var checkbox = new Ext.grid.CheckboxSelectionModel();
	var columns = new Ext.grid.ColumnModel([
		checkbox,
		{header:"序号",width:200,dataIndex:"musicId",sortable:true,editor:new Ext.form.Label()},
		{header:"歌曲名字",dataIndex:"title",sortable:true,editor:new Ext.form.TextField()},
		{header:"歌手名字",dataIndex:"singer",sortable:true,editor:new Ext.form.TextField()},
		{header:"歌曲地址",dataIndex:"address",sortable:true,editor:new Ext.form.TextField()},
		{header:"歌曲歌词",dataIndex:"lyrics",sortable:true,editor:new Ext.form.TextField()}
	]);
	
	var grid = new Ext.grid.EditorGridPanel({
		renderTo:'tables',
		title:'菜单列表编辑',
		cm:columns,
		selModel:checkbox,
		autoHeight:true,
		store:store,
		tbar:[
			{
				text:'导航栏',
				cls:'x-btn-text-icon',
				handler:function(){
					location.href='home/index.wkm';
				}
				
			},{
				text:'添加音乐',
				cls:'x-btn-text-icon',
				handler:function(){
					location.href='music/home.wkm';
				}
				
			},{
				text:'保存',
				cls:'x-btn-text-icon',
				handler:function(){
					var store = grid.getStore();
					var modified = store.modified.slice(0);
					var jsonArray = [];
					var musicId = '';
					var title = '';
					var singer = '';
					var address = '';
					var lyrics = '';
					Ext.each(modified,function(m){
						//alert(m.data.ADDRESS);//读取当前被修改的记录的地址  
						//m.data中保存的是当前Recored的所有字段的值json，不包含结构信息  
						jsonArray.push(m.data);
						musicId = m.get('musicId');
						title = m.get('title');
						singer = m.get('singer');
						address =  m.get('address');
						lyrics = m.get('lyrics');
					});
					if(jsonArray.length === 0){
						Ext.Msg.alert('信息提示框','没有对表格进行修改!');
						return ;
					}
					Ext.Msg.confirm('信息提示框','真的要保存修改行吗?',function(btn){
						if(btn == 'yes'){
							Ext.Ajax.request({
								method:'POST',
								url:'music/updateMusic.wkm',
								success:function(response,options){
									Ext.Msg.alert('信息提示框','修改成功!');
								},
								failure:function(response,options){
									Ext.Msg.alert('信息提示框',Ext.util.JSON.encode(jsonArray));
								},
								//jsonData:Ext.util.JSON.encode(jsonArray),
								params:{'musicId':musicId,'title':title,'singer':singer,'address':address,'lyrics':lyrics}
							});
						}
					});
					//Ext.Msg.alert('信息提示框',jsonArray.length);
				}
			},{
				text:'删除',
				cls:'x-btn-text-icon',
				handler:function(){
					var selectModel = grid.getSelectionModel();
					if(selectModel.hasSelection()){
						Ext.Msg.confirm('信息提示框','真的要删除选中行吗?',function(btn){
							if(btn == 'yes'){
								var record = selectModel.getSelected();//获取单元格的行列
								var obj = record.get('musicId');
								Ext.Ajax.request({
									method:'POST',
									url:'music/deleteMusic.wkm',
									success:function(response,options){
										Ext.Msg.alert('信息提示框','删除成功!');
									},
									failure:function(response,options){
										Ext.Msg.alert('信息提示框','删除失败!');
									},
									params:{'musicId':obj}
								});
								grid.getStore().remove(record);
							} else {
								Ext.Msg.alert('信息提示框','已经取消!');
							}
						});
					}
					//Ext.Msg.alert("信息提示框","请先选中要删除的行!");
				}
			}
		]
	});

	var w = new Ext.Window({
		contentEl:'win',
		items:grid
	});
	w.show();
});